package com.ikeke.es;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;

import java.net.InetAddress;
import java.util.Iterator;
import java.util.Map;

public class SearchIndex {
    private TransportClient client = null;

    @Before
    public void init() throws Exception{
        Settings settings = Settings.builder().put("cluster.name","my-elasticsearch").build();

        //创建一个client对象
        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9301))
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9302))
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9303));
    }

    @Test
    public void testSearchById() throws  Exception {

        QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("1","2");

        search(queryBuilder);

    }
    private void search( QueryBuilder queryBuilder) throws Exception{
        SearchResponse searchResponse = client.prepareSearch("index_hello").setTypes("article").setQuery(queryBuilder)
                //设置分页信息
                .setFrom(0).setSize(2)
                .get();

        SearchHits searchHits = searchResponse.getHits();


        System.out.println("查询结果的记录数:"+searchHits.getTotalHits());
        System.out.println(searchHits.getHits());
        Iterator<SearchHit> iterator =  searchHits.iterator();
        while (iterator.hasNext()){
            SearchHit searchHit = iterator.next();
            System.out.println(searchHit.getSourceAsString());
            System.out.println("--------------------文档属性============");
            Map<String, Object> source = searchHit.getSource();
            System.out.println(source.get("id"));
            System.out.println(source.get("title"));
            System.out.println(source.get("content"));
        }
    }

    @Test
    public void testSearchByTerm() throws  Exception {

        QueryBuilder queryBuilder = QueryBuilders.termQuery("title","9月");


        search(queryBuilder);

    }


    @Test
    public void testQueryStringQuery() throws  Exception {

        QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("的哥").defaultField("title");
//        search(queryBuilder);

        search2(queryBuilder,"title");

    }

    private void search2( QueryBuilder queryBuilder, String highlightField) throws Exception{
        //高亮信息对象
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        //高亮显示的字段
        highlightBuilder.field(highlightField);
        highlightBuilder.preTags("<em>");
        highlightBuilder.postTags("</em>");
        SearchResponse searchResponse = client.prepareSearch("index_hello").setTypes("article").setQuery(queryBuilder)
                //设置分页信息
                .setFrom(0).setSize(5)
                //设置高亮信息
                .highlighter(highlightBuilder)
                .get();

        SearchHits searchHits = searchResponse.getHits();


        System.out.println("查询结果的记录数:"+searchHits.getTotalHits());
        System.out.println(searchHits.getHits());
        Iterator<SearchHit> iterator =  searchHits.iterator();
        while (iterator.hasNext()){
            SearchHit searchHit = iterator.next();
            System.out.println(searchHit.getSourceAsString());
            System.out.println("--------------------文档属性============");
            Map<String, Object> source = searchHit.getSource();

            System.out.println(source.get("id"));
            System.out.println(source.get("title"));
            System.out.println(source.get("content"));
            System.out.println("*****************高亮结果******************");
            Map<String,HighlightField> highlightFields = searchHit.getHighlightFields();
            System.out.println(highlightFields);
            HighlightField field= highlightFields.get(highlightField);
            Text[] fragments = field.getFragments();
            if(fragments != null){
                String  str= fragments[0].toString();
                System.out.println(str);
            }
        }
    }
}
